import hashlib
import time

import cv2
# 灰度
# def huidu():
# # 读图 去色：cv2.IMREAD_GRAYSCALE
# img=cv2.imread('test.png',cv2.IMREAD_GRAYSCALE)
#
# # 写图
# cv2.imwrite('test2.png',img)

from PIL import ImageDraw, Image, ImageFont


# 水印
# def suiyin():
# 读图
#     img=Image.open('test.png')
#     print(img.format,img.size,img.mode)
#     width=img.size[0]
#     height=img.size[1]
#     # 生成画笔
#     draw=ImageDraw.Draw(img)
#     draw.text((width-30,height-10),'1907',fill=(76,234,124,180))
#     img.show()

# 图像压缩
# def yasuo():
#     img=cv2.imread('./test.png')
#     # png压缩操作
#     cv2.imwrite('./test2.png',img,[cv2.IMWRITE_PNG_COMPRESSION,9])
#     img = cv2.imread('./trapped4_8k.jpg')
#     cv2.imwrite('./trapped4_8k2.jpg', img, [cv2.IMWRITE_JPEG_OPTIMIZE, 90])
def make_password(mypass):
    # 生成MD5对象
    md5 = hashlib.md5()
    # 转码操作
    mypass_utf8 = str(mypass).encode(encoding="utf-8")
    # 加密操作
    md5.update(mypass_utf8)
    # 返回密文
    return md5.hexdigest()

def cn_logo(img_name):
    img = Image.open(img_name)
    # 获取宽高
    width, height = img.size
    # 根据图片大小定义logo大小
    text = '美多商城'
    font = ImageFont.truetype('msyh.ttc', (width - height) // 10)
    # 获取字体宽高
    font_width, font_height = font.getsize(text)
    # 使用画笔
    draw = ImageDraw.Draw(img)
    # 写入文本
    draw.text((width - font_width, height - font_height - 10), text, (100, 100, 100), font=font)
    filename = make_password(img_name) + str((int(round(time.time() * 1000)))) + ".jpg"
    img.save(os.path.join(UPLOAD_ROOT,'',filename))

    return filename


cn_logo('trapped4_8k.jpg')